package com.taijia.chapter7;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.taijia.chapter3.Student;

/**
 * Map test - auto boxing deboxing
 * 
 * @author taijia
 * @since 2014年11月24日
 *
 */
public class MapTest {
	
	private static final int ONE = 1;
	
	/**
	 * 自动打包解包
	 */
	private static void autoBoxing() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("one", 1);
		map.put("two", 2.0);
		map.put("tree", 3);
		map.put("four", 4);
		System.out.println(map.get("one") instanceof Integer);
		System.out.println(map.get("one") instanceof Object);
		System.out.println(map.get("two") instanceof Double);
		System.out.println(map.get("two") instanceof Integer);
		System.out.println(map.get("two").getClass());
		Constructor<?>[] constructors = new Student().getClass().getConstructors();
		Method[] methods =  new Student().getClass().getDeclaredMethods();
		System.out.println(JSON.toJSONString(constructors));
		System.out.println(JSON.toJSONString(methods));
	}
	
	/**
	 * 各个键的数量
	 */
	private static void countWords() {
		Map<String,Object> m = new HashMap<String, Object>();
		String[] strs = {"11","dd","cc","ss","ll","ll"};
		for (String str : strs) {
			Integer freq = (Integer)m.get(str);
			m.put(str, freq == null ? ONE : freq+1);
		}
		System.out.println("Map size: " + m.size());
		System.out.println("words: " + JSON.toJSONString(m));
	}
	
	public static void main(String[] args) {
		autoBoxing();
		countWords();
	}
	
}
